/**
 * 
 */
package fr.gfi.agraf.vue.action.ajax;

import java.util.List;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionSupport;

import fr.gfi.agraf.exception.AgrafException;
import fr.gfi.agraf.service.IComposantService;
import fr.gfi.agraf.utils.PropertiesUtils;
import fr.gfi.agraf.utils.SpringFactory;
import fr.gfi.agraf.vue.bean.ComposantDTO;

/**
 * @author Zakaria
 *
 */
public class AjaxGetListeComposantsForProfil extends ActionSupport {
	
	/**
	 * Logger
	 */
	private final static Logger LOGGER = Logger.getLogger(AjaxGetListeComposantsForProfil.class);

	private static final String ERREUR_SAISIE_ID = "erreur.saisie.id";

	private static final String COMPOSANT_SERVICE = "ComposantService";

	/**
	 * 
	 */
	private static final long serialVersionUID = 2506567016265288043L;

	/**
	 * Id récupéré de la page d'appel
	 */
	private Long idProfil;

	private List<ComposantDTO> composantList;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {
		// Spring, recupere le service composant
		final IComposantService composantService = (IComposantService) SpringFactory.getApplicationContext()
				.getBean(COMPOSANT_SERVICE);

		try {
			// fait appel au service composant
			composantList = composantService.getAllComposantToModifyProfil(idProfil);
		} catch (final AgrafException ae) {
			// Recupére le message d'erreur
			final String messageErreur = PropertiesUtils.getProperty(ae.getKey());
			// Ajoute action error
			addActionError(messageErreur);
			LOGGER.error(messageErreur,ae);
			return ERROR;
		}
		return SUCCESS;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#validate()
	 */
	@Override
	public void validate() {
		if (idProfil == null || idProfil == 0) {
			addActionError(ERREUR_SAISIE_ID);
		}
		super.validate();
	}

	/**
	 * @return the composantList
	 */
	public List<ComposantDTO> getComposantList() {
		return composantList;
	}

	/**
	 * @param composantList
	 *            the composantList to set
	 */
	public void setComposantList(final List<ComposantDTO> composantList) {
		this.composantList = composantList;
	}


	/**
	 * @param idProfil
	 *            the idProfil to set
	 */
	public void setIdProfil(final Long idProfil) {
		this.idProfil = idProfil;
	}

}
